home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Offline Browsing / HTTrack.exe / data1.cab / Sources / src / htsalias.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-04-28  |  15.0 KB  |  492 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: htsalias.c subroutines:                                */
  34. /*       alias for command-line options and config files        */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include "htsalias.h"
  42. #include "htsglobal.h"
  43. void linput(FILE* fp,char* s,int max);
  44. void hts_lowcase(char* s);
  45.  
  46. #define _NOT_NULL(a) ( (a!=NULL) ? (a) : "" )
  47. #define is_realspace(c) (strchr(" \x0d\x0a\x09",(c))!=NULL)
  48.  
  49. // COPY OF cmdl_ins in htsmain.c
  50. // Insert a command in the argc/argv
  51. #define cmdl_ins(token,argc,argv,buff,ptr) \
  52.   { \
  53.   int i; \
  54.   for(i=argc;i>0;i--)\
  55.   argv[i]=argv[i-1];\
  56.   } \
  57.   argv[0]=(buff+ptr); \
  58.   strcpy(argv[0],token); \
  59.   ptr += (strlen(argv[0])+1); \
  60.   argc++
  61. // END OF COPY OF cmdl_ins in htsmain.c
  62.  
  63.  
  64. /*
  65.   Aliases for command-line and config file definitions
  66.   These definitions can be used:
  67.   in command line:
  68.   --sockets=8       --cache=0
  69.   --sockets 8       --cache off
  70.                     --nocache
  71.   -c8               -C0
  72.   in config file:
  73.   sockets=8         cache=0
  74.   set sockets 8     cache off
  75.  
  76. */
  77. /*
  78.   single : no options
  79.   param  : this option allows a number parameter (1, for example) and can be mixed with other options (R1C1c8)
  80.   param1 : this option must be alone, and needs one distinct parameter (-P <path>)
  81.   param0 : this option must be alone, but the parameter should be put together (+*.gif)
  82. */
  83. const char hts_optalias[][4][64] = {
  84.   {"connections","-c","param","number of simultaneous connections allowed"},
  85.   {"cache","-C","param","number of retries for non-fatal errors"},
  86.  
  87.   
  88.   /*   {"","","",""}, */
  89.   {"path","-O","param1","output path"},
  90.   /*{"top-path","-%O","param1","default top path"},*/
  91.   {"mirror","-w","single",""},
  92.   {"mirror-wizard","-W","single",""},
  93.   {"get-files","-g","single",""},
  94.   {"quiet","-q","single",""},
  95.   {"mirrorlinks","-Y","single",""},
  96.   {"proxy","-P","param1","proxy name:port"},
  97.   {"httpproxy-ftp","-%f","param",""},
  98.   {"depth","-r","param",""},{"recurse-levels","-r","param",""},
  99.   {"ext-depth","-%e","param",""},
  100.   {"max-files","-m","param",""},
  101.   {"max-size","-M","param",""},
  102.   {"max-time","-E","param",""},
  103.   {"max-rate","-A","param",""},
  104.   {"max-pause","-G","param",""},
  105.   {"sockets","-c","param","number of simultaneous connections allowed"},
  106.   {"timeout","-T","",""},
  107.   {"retries","-R","param","number of retries for non-fatal errors"},
  108.   {"min-rate","-J","param",""},
  109.   {"host-control","-H","param",""},
  110.   {"extended-parsing","-%P","param",""},
  111.   {"near","-n","single",""},
  112.   {"test","-t","single",""},
  113.   {"list","-%L","param1",""},
  114.   {"language","-%l","param1",""}, {"lang","-%l","param1",""},
  115.   {"structure","-N","param",""}, {"user-structure","-N","param1",""},
  116.   {"long-names","-L","param",""},
  117.   {"keep-links","-K","param",""},
  118.   {"replace-external","-x","single",""},
  119.   {"generate-errors","-o","single",""},
  120.   {"purge-old","-X","param",""},
  121.   {"cookies","-b","param",""},
  122.   {"check-type","-u","param",""},
  123.   {"parse-java","-j","param",""},
  124.   {"robots","-s","param",""},
  125.   {"http-10","-%h","single",""},
  126.   {"tolerant","-%B","single",""},
  127.   {"updatehack","-%s","single",""}, {"sizehack","-%s","single",""},
  128.   {"user-agent","-F","param1","user-agent identity"},
  129.   {"footer","-%F","param1",""},
  130.   {"cache","-C","param",""},
  131.   {"store-all-in-cache","-k","single",""},
  132.   {"do-not-recatch","-%n","single",""},
  133.   {"do-not-log","-Q","single",""},
  134.   {"extra-log","-z","single",""},
  135.   {"debug-log","-Z","single",""},
  136.   {"verbose","-v","single",""},
  137.   {"file-log","-f","single",""},
  138.   {"single-log","-f2","single",""},
  139.   {"index","-I","single",""},
  140.   {"search-index","-%I","single",""},
  141.   {"priority","-p","param",""},
  142.   {"debug-headers","-%H","single",""},
  143.   {"userdef-cmd","-V","param1",""},
  144.   {"structure","-N","param1","user-defined structure"},
  145.   {"usercommand","-V","param1","user-defined command"},
  146.   {"display","-%v","single","show files transfered and other funny realtime information"},
  147.   /* */
  148.  
  149.  
  150.   
  151.   /* STANDARD ALIASES */
  152.   {"spider","-p0C0I0t","single",""},
  153.   {"testsite","-p0C0I0t","single",""},
  154.   {"testlinks","-r1p0C0I0t","single",""}, {"test","-r1p0C0I0t","single",""}, {"bookmark","-r1p0C0I0t","single",""},
  155.   {"mirror","-w","single",""},
  156.   {"testscan","-p0C0I0Q","single",""}, {"scan","-p0C0I0Q","single",""}, {"check","-p0C0I0Q","single",""},
  157.   {"skeleton","-p1","single",""},
  158.   {"get","-qg","single",""},
  159.   {"update","-iC2","single",""},
  160.   {"continue","-iC1","single",""}, {"restart","-iC1","single",""},
  161.   {"sucker","-r999","single",""},
  162.   {"help","-h","single",""}, {"documentation","-h","single",""}, {"doc","-h","single",""},
  163.   {"wide","-c32","single",""},
  164.   {"tiny","-c1","single",""},
  165.   {"ultrawide","-c48","single",""},
  166.   {"http10","-%h","single",""},
  167.   {"filelist","-%L","single",""}, {"list","-%L","single",""},
  168.   /* END OF ALIASES */
  169.  
  170.   /* Filters */
  171.   {"allow","+","param0","allow filter"},
  172.   {"deny","-","param0","deny filter"},
  173.   /* */
  174.  
  175.   /* URLs */
  176.   {"add","","param0","add URLs"},
  177.   /* */
  178.  
  179.   /* Specific */
  180.   {"user","-%U","param1","output path"},
  181.   /* */
  182.  
  183.   /* Internal */
  184.   {"catchurl","--catchurl","single","catch complex URL through proxy"},
  185.   {"updatehttrack","--updatehttrack","single","update HTTrack Website Copier"},
  186.   {"clean","--clean","single","clean up log files and cache"},
  187.   {"tide","--clean","single","clean up log files and cache"},
  188.   /* */
  189.  
  190.   {"","","",""}
  191. };
  192.  
  193.  
  194. /* 
  195.   Check for alias in command-line 
  196.   argc,argv     as in main()
  197.   n_arg         argument position
  198.   return_argv   a char[2][] where to put result
  199.   return_error  buffer in case of syntax error
  200.  
  201.   return value: number of arguments treated (0 if error)
  202. */
  203. int optalias_check(int argc,const char * const * argv,int n_arg,
  204.                    int* return_argc,char** return_argv,
  205.                    char* return_error) {
  206.   return_error[0]='\0';
  207.   *return_argc=1;
  208.   if (argv[n_arg][0]=='-')
  209.   if (argv[n_arg][1]=='-') {
  210.     char command[1000];
  211.     char param[1000];
  212.     char addcommand[256];
  213.     /* */
  214.     char* position;
  215.     int need_param=1;
  216.     //int return_param=0;
  217.     int pos;
  218.     command[0]=param[0]=addcommand[0]='\0';
  219.  
  220.     /* --sockets=8 */
  221.     if ( (position=strchr(argv[n_arg],'=')) ) {
  222.       /* Copy command */
  223.       strncat(command,argv[n_arg]+2,(int) (position - (argv[n_arg]+2)) );
  224.       /* Copy parameter */
  225.       strcpy(param,position+1);
  226.     }
  227.     /* --nocache */
  228.     else if (strncmp(argv[n_arg]+2,"no",2)==0) {
  229.       strcpy(command,argv[n_arg]+4);
  230.       strcpy(param,"0");
  231.     }
  232.     /* --sockets 8 */
  233.     else {
  234.       if (strncmp(argv[n_arg]+2,"wide-",5)==0) {
  235.         strcpy(addcommand,"c32");
  236.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  237.       } else if (strncmp(argv[n_arg]+2,"tiny-",5)==0) {
  238.         strcpy(addcommand,"c1");
  239.         strcpy(command,strchr(argv[n_arg]+2,'-')+1);
  240.       } else
  241.         strcpy(command,argv[n_arg]+2);
  242.       need_param=2;
  243.     }
  244.  
  245.     /* Now solve the alias */
  246.     pos=optalias_find(command);
  247.     if (pos>=0) {
  248.       /* Copy real name */
  249.       strcpy(command,hts_optalias[pos][1]);
  250.       /* With parameters? */
  251.       if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  252.         /* Copy parameters? */
  253.         if (need_param == 2) {
  254.           if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  255.             sprintf(return_error,
  256.               "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  257.               command,command,_NOT_NULL(optalias_help(command)));
  258.             return 0;
  259.           }
  260.           strcpy(param,argv[n_arg+1]);
  261.           need_param=2;
  262.         }
  263.       } else
  264.         need_param=1;
  265.  
  266.       /* Final result */
  267.  
  268.       /* Must be alone (-P /tmp) */
  269.       if (strcmp(hts_optalias[pos][2],"param1")==0) {
  270.         strcpy(return_argv[0],command);
  271.         strcpy(return_argv[1],param);
  272.         *return_argc=2;     /* 2 parameters returned */
  273.       } 
  274.       /* Alone with parameter (+*.gif) */
  275.       else if (strcmp(hts_optalias[pos][2],"param0")==0) {
  276.         /* Command */
  277.         strcpy(return_argv[0],command);
  278.         strcat(return_argv[0],param);
  279.       }
  280.       /* Together (-c8) */
  281.       else {
  282.         /* Command */
  283.         strcpy(return_argv[0],command);
  284.         /* Parameters accepted */
  285.         if (strncmp(hts_optalias[pos][2],"param",5)==0) {
  286.           /* --cache=off or --index=on */
  287.           if (strcmp(param,"off")==0)
  288.             strcat(return_argv[0],"0");
  289.           else if (strcmp(param,"on")==0) {
  290.             // on is the default
  291.             // strcat(return_argv[0],"1");
  292.           } else
  293.             strcat(return_argv[0],param);
  294.         }
  295.         *return_argc=1;     /* 1 parameter returned */
  296.       }
  297.     } else {
  298.       sprintf(return_error,"Unknown option: %s\n",command);
  299.       return 0;
  300.     }
  301.     return need_param;
  302.   }
  303.  
  304.   /* Check -P <path> */
  305.   {
  306.     int pos;
  307.     if ((pos=optreal_find(argv[n_arg]))>=0) {
  308.       if ( (strcmp(hts_optalias[pos][2],"param1")==0) || (strcmp(hts_optalias[pos][2],"param0")==0)) {
  309.         if ((n_arg+1>=argc) || (argv[n_arg+1][0]=='-')) {  /* no supplemental parameter */
  310.           sprintf(return_error,
  311.             "Syntax error:\n\tOption %s needs to be followed by a parameter: %s <param>\n\t%s\n",
  312.             argv[n_arg],argv[n_arg],_NOT_NULL(optalias_help(argv[n_arg])));
  313.           return 0;
  314.         }
  315.         /* Copy parameters */
  316.         strcpy(return_argv[0],argv[n_arg]);
  317.         strcpy(return_argv[1],argv[n_arg+1]);
  318.         /* And return */
  319.         *return_argc=2;     /* 2 parameters returned */
  320.         return 2;           /* 2 parameters used */
  321.       }
  322.     }
  323.   }
  324.   
  325.   /* Copy and return other unknown option */
  326.   strcpy(return_argv[0],argv[n_arg]);
  327.   return 1;
  328. }
  329.  
  330. /* Finds the <token> option alias and returns the index, or -1 if failed */
  331. int optalias_find(const char* token) {
  332.   if (token[0] != '\0') {
  333.     int i=0;
  334.     while(hts_optalias[i][0][0] != '\0') {
  335.       if (strcmp(token,hts_optalias[i][0])==0) {
  336.         return i;
  337.       }
  338.       i++;
  339.     }
  340.   }
  341.   return -1;
  342. }
  343.  
  344. /* Finds the <token> real option and returns the index, or -1 if failed */
  345. int optreal_find(const char* token) {
  346.   if (token[0] != '\0') {
  347.     int i=0;
  348.     while(hts_optalias[i][0][0] != '\0') {
  349.       if (strcmp(token,hts_optalias[i][1])==0) {
  350.         return i;
  351.       }
  352.       i++;
  353.     }
  354.   }
  355.   return -1;
  356. }
  357.  
  358. const char* optreal_value(int p) {
  359.   return hts_optalias[p][1];
  360. }
  361. const char* optalias_value(int p) {
  362.   return hts_optalias[p][0];
  363. }
  364. const char* opttype_value(int p) {
  365.   return hts_optalias[p][2];
  366. }
  367. const char* opthelp_value(int p) {
  368.   return hts_optalias[p][3];
  369. }
  370.  
  371. /* Help for option <token>, empty if not available, or NULL if unknown <token> */
  372. const char* optalias_help(const char* token) {
  373.   int pos=optalias_find(token);
  374.   if (pos>=0)
  375.     return hts_optalias[pos][3];
  376.   else
  377.     return NULL;
  378. }
  379.  
  380. /* Include a file to the current command line */
  381. /* example:
  382.   set sockets 8
  383.   index on
  384.   allow *.gif
  385.   deny ad.*
  386. */
  387. int optinclude_file(const char* name,
  388.                     int* argc,char** argv,char* x_argvblk,int* x_ptr) {
  389.   FILE* fp;
  390.   fp=fopen(name,"rb");
  391.   if (fp) {
  392.     char line[256];
  393.     int insert_after=1;       /* first, insert after program filename */
  394.     while(!feof(fp)) {
  395.       char *a,*b;
  396.       int result;
  397.       
  398.       /* read line */
  399.       linput(fp,line,250);
  400.       hts_lowcase(line);
  401.       if (strnotempty(line)) {
  402.         /* no comment line: # // ; */
  403.         if (strchr("#/;",line[0])==NULL) {
  404.           /* right trim */
  405.           a=line+strlen(line)-1;
  406.           while(is_realspace(*a)) *(a--) = '\0';
  407.           /* jump "set " and spaces */
  408.           a=line;
  409.           while(is_realspace(*a)) a++;
  410.           if (strncmp(a,"set",3)==0) {
  411.             if (is_realspace(*(a+3))) {
  412.               a+=4;
  413.             }
  414.           }
  415.           while(is_realspace(*a)) a++;
  416.           /* delete = ("sockets=8") */
  417.           if ( (b=strchr(a,'=')) )
  418.             *b=' ';
  419.           
  420.           /* isolate option and parameter */
  421.           b=a;
  422.           while( (!is_realspace(*b)) && (*b) ) b++;
  423.           if (*b) {
  424.             *b='\0';
  425.             b++;
  426.           }
  427.           /* a is now the option, b the parameter */
  428.           
  429.           {
  430.             int return_argc;
  431.             char return_error[256];
  432.             char  _tmp_argv[4][HTS_CDLMAXSIZE];
  433.             char*  tmp_argv[4];
  434.             tmp_argv[0]=_tmp_argv[0]; tmp_argv[1]=_tmp_argv[1]; tmp_argv[2]=_tmp_argv[2]; tmp_argv[3]=_tmp_argv[3];
  435.             strcpy(tmp_argv[0],"--");
  436.             strcat(tmp_argv[0],a);
  437.             strcpy(tmp_argv[1],b);
  438.             
  439.             result=optalias_check(2,(const char * const *)tmp_argv,0,
  440.               &return_argc,(tmp_argv+2),
  441.               return_error);
  442.             if (!result) {
  443.               printf("%s\n",return_error);
  444.             } else {
  445.               int insert_after_argc;
  446.               /* Insert parameters BUT so that they can be in the same order */
  447.               /* temporary argc: Number of parameters after minus insert_after_argc */
  448.               insert_after_argc=(*argc)-insert_after;
  449.               cmdl_ins((tmp_argv[2]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  450.               *argc=insert_after_argc+insert_after;
  451.               insert_after++;
  452.               /* Second one */
  453.               if (return_argc>1) {
  454.                 insert_after_argc=(*argc)-insert_after;
  455.                 cmdl_ins((tmp_argv[3]),insert_after_argc,(argv+insert_after),x_argvblk,(*x_ptr));
  456.                 *argc=insert_after_argc+insert_after;
  457.                 insert_after++;
  458.               }
  459.               /* increment to nbr of used parameters */
  460.               /* insert_after+=result; */
  461.             }
  462.           }
  463.         }
  464.         
  465.       }
  466.     }
  467.     fclose(fp);
  468.     return 1;
  469.   }
  470.   return 0;
  471. }
  472.  
  473. /* Get home directory, empty if failed */
  474. /* example: /home/smith */
  475. char* hts_gethome(void) {
  476.   char* home = getenv( "HOME" );
  477.   if (home)
  478.     return home;
  479.   else
  480.     return ".";
  481. }
  482.  
  483. /* Convert ~/foo into /home/smith/foo */
  484. void expand_home(char* str) {
  485.   if (str[0] == '~') {
  486.     char tempo[1024];
  487.     strcpy(tempo,hts_gethome());
  488.     strcat(tempo,str+1);
  489.     strcpy(str,tempo);
  490.   }
  491. }
  492.